home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / vert_t3d.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  106 lines

  1. ; $Id: vert_t3d.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       VERT_T3D
  8. ;
  9. ; PURPOSE:
  10. ;       This function tranforms 3-D points by a 4x4 transformation matrix.
  11. ;       The 3-D points are typically an array of polygon vertices that were
  12. ;       generated by SHADE_VOLUME or MESH_OBJ.
  13. ;
  14. ; CATEGORY:
  15. ;       Graphics.
  16. ;
  17. ; CALLING SEQUENCE:
  18. ;       result = VERT_T3D(vertex_list)
  19. ;
  20. ; INPUTS:
  21. ;       Vertex_List:
  22. ;               A vector of the form [x, y, z], or a [3, n] array of 3-D
  23. ;               coordinates to transform.
  24. ;
  25. ; KEYWORD PARAMETERS:
  26. ;
  27. ;       Matrix:
  28. ;               The 4x4 transformation matrix to use. The default is to use
  29. ;               the system viewing matrix (!P.T). (See the "T3D" procedure). 
  30. ;
  31. ;       No_Copy:
  32. ;               Normally, a COPY of Vertex_List is transformed and the
  33. ;               original vertex_list is preserved. If No_Copy is set, however,
  34. ;               then the original Vertex_List will be undefined AFTER the call
  35. ;               to VERT_T3D. Using the No_Copy mode will require less memory.
  36. ;
  37. ;       No_Divide:
  38. ;               Normally, when a [x, y, z, 1] vector is transformed by a 4x4
  39. ;               matrix, the final homogeneous coordinates are obtained by
  40. ;               dividing the x, y, and z components of the result vector by
  41. ;               the fourth element in the result vector. Setting the No_Divide
  42. ;               keyword will prevent VERT_T3D from performing this division.
  43. ;               In some cases (usually when a perspective transformation is
  44. ;               involved) the fourth element in the result vector can be very
  45. ;               close to (or equal to) zero.
  46. ;
  47. ;       Save_Divide:
  48. ;               Set this keyword to a named variable to receive the fourth
  49. ;               element of the transformed vector(s). If Vertex_List is a
  50. ;               vector then Save_Divide is a scalar. If Vertex_List is a
  51. ;               [3, n] array then Save_Divide is an array of n elements.
  52. ;               This keyword only has effect when the No_Divide keyword is set.
  53. ;
  54. ; OUTPUTS:
  55. ;       This function returns the transformed coordinate(s). The returned
  56. ;       array has the same size and dimensions as Vertex_List.
  57. ;
  58. ; PROCEDURE:
  59. ;       Before performing the transformation, the [3, n] Vertex_List is padded
  60. ;       to produce a [4, n] array with 1's in the last column. After the
  61. ;       transformation, the first three columns of the array are divided by
  62. ;       the fourth column (unless the No_Divide keyword is set). The fourth
  63. ;       column is then stripped off (or saved in the Save_Divide keyword)
  64. ;       before returning.
  65. ;
  66. ; EXAMPLE:
  67. ;       Transform four points representing a square in the x-y plane by first
  68. ;       translating +2.0 in the positive X direction, and then rotating 60.0
  69. ;       degrees about the Y axis.
  70. ;
  71. ;               points = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], $
  72. ;                         [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]]
  73. ;               T3d, /Reset
  74. ;               T3d, Translate=[2.0, 0.0, 0.0]
  75. ;               T3d, Rotate=[0.0, 60.0, 0.0]
  76. ;               points = VERT_T3D(points)
  77. ;
  78. ; MODIFICATION HISTORY:
  79. ;       Written by:     Daniel Carr, Thu Mar 31 15:58:07 MST 1994
  80. ;-
  81.  
  82. FUNCTION VERT_T3D, vertex_list, Matrix=matrix, No_Copy=no_copy, $
  83.                    No_Divide=no_divide, Save_Divide=save_divide
  84.  
  85. sz_vertex = Size(vertex_list)
  86. n_verts = sz_vertex[2]
  87.  
  88. IF (N_Elements(matrix) LE 0L) THEN matrix = !P.T
  89.  
  90. IF (Keyword_Set(no_copy)) THEN BEGIN
  91.    ret_list = Transpose( $
  92.       Transpose([Temporary(vertex_list), Replicate(1.0, 1L, n_verts)]) # matrix)
  93. ENDIF ELSE BEGIN
  94.    ret_list = $
  95.       Transpose(Transpose([vertex_list, Replicate(1.0, 1L, n_verts)]) # matrix)
  96. ENDELSE
  97.  
  98. IF (Keyword_Set(no_divide)) THEN BEGIN
  99.    save_divide = ret_list[3, *]
  100.    RETURN, [ret_list[0, *], ret_list[1, *], ret_list[2, *]]
  101. ENDIF ELSE $
  102.    RETURN, [(ret_list[0, *] / ret_list[3, *]), $
  103.             (ret_list[1, *] / ret_list[3, *]), $
  104.             (ret_list[2, *] / ret_list[3, *])]
  105. END
  106.